<template>
  <div id="labelGroupInfo">
    <el-form ref="form" :model="form" label-width="120px" class="form">
      <el-form-item label="标签组名称" class="input">
        <el-input v-model="form.labelGroupName"></el-input>
      </el-form-item>
      <el-form-item label="是否唯一" class="input">
        <el-radio v-model="form.isUnique" label="1">是</el-radio>
        <el-radio v-model="form.isUnique" label="0">否</el-radio>
      </el-form-item>
    </el-form>
    <div class="buttonBox">
      <div class="button" @click="save" v-if="labelGroupId">保存</div>
      <div class="button" @click="submit" v-if="!labelGroupId">完成</div>
    </div>
  </div>
</template>
<script>
import { labelGroupAdd, labelGroupDeatil, labelGroupUpdate } from 'api/label'
export default {
  props: ['labelGroupId'],
  data() {
    return {
      form: {
        labelGroupName: '',
        isUnique: '0'
      }
    }
  },
  created() {
    if (this.labelGroupId) {
      this.getGroupDetail()
    }
  },
  methods: {
    async getGroupDetail() {
      try {
        let back = await labelGroupDeatil(this.labelGroupId)
        back = back.data
        if (back.resultCode === '200') {
          this.form.labelGroupName = back.result.labelGroupName
          this.form.isUnique = String(back.result.isUnique)
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    },
    async submit() {
      try {
        let back = await labelGroupAdd(this.form)
        back = back.data
        // 这里后台有问题~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        if (back.resultCode === '200' || back.resultCode === null) {
          this.$router.push({ name: 'labelGroupList' })
        } else {
          this.$alert(back.msg)
        }
      } catch (err) {
        this.$alert('请求出错', '提示')
      }
    },
    async save() {
      try {
        const parm = { ...this.form }
        parm.labelGroupId = this.labelGroupId
        let back = await labelGroupUpdate(parm)
        back = back.data
        if (back.resultCode === '200') {
          this.$router.push({ name: 'labelGroupList' })
        } else {
          this.$alert(back.msg)
        }
      } catch (err) {
        this.$alert('请求从出错', '提示')
      }
    }
  }
}
</script>
<style lang="scss" scoped>
  #labelGroupInfo{
    width:40%;
    .buttonBox{
      overflow: hidden;
      .button{
        float: left;
        width:200px;
        height:40px;
        line-height: 40px;
        border-radius: 5px;
        margin:40px ;
        color:white;
        background: #409EFF;
        text-align: center;
        cursor: pointer;
      }
    }
  }
</style>
